const { Model, DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    class Permission extends Model {
        static associate(models) {
            // 定义关联关系
            Permission.belongsToMany(models.Role, {
                through: models.RolePermission,
                foreignKey: 'permission_id',
                otherKey: 'role_id',
                as: 'roles'
            });
        }
    }

    Permission.init(
        {
            id: {
                type: DataTypes.BIGINT,
                autoIncrement: true,
                primaryKey: true,
                comment: '权限ID'
            },
            permission_key: {
                type: DataTypes.STRING(100),
                allowNull: false,
                unique: true,
                comment: '权限标识'
            },
            name: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '权限名称'
            },
            description: {
                type: DataTypes.TEXT,
                comment: '权限描述'
            },
            parent_id: {
                type: DataTypes.BIGINT,
                defaultValue: 0,
                comment: '父级ID'
            }
        },
        {
            sequelize,
            modelName: 'Permission',
            tableName: 'permissions',
            timestamps: true,
            paranoid: true,
            underscored: true
        }
    );

    return Permission;
}; 